package ejb.managerbeans;

import javax.ejb.Stateless;

import ejb.dto.HotelDTO;
import ejb.entities.*;
import ejb.utility.ConvertDTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class HotelManagerBean
 */
@Stateless
public class HotelManagerBean implements HotelManagerBeanLocal {

	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;

	/**
	 * Default constructor.
	 */
	public HotelManagerBean() {

	}

	public List<Hotel> getAllHotels() {

		return em.createNamedQuery(Hotel.FIND_ALL, Hotel.class).getResultList();

	}

	@Override
	public List<HotelDTO> getAllHotelDTO() {
		List<HotelDTO> hotelsDTO = new ArrayList<HotelDTO>();
		List<Hotel> hotels = getAllHotels();

		for (Hotel h : hotels)
			hotelsDTO.add(ConvertDTO.convertToHotelDTO(h));

		return hotelsDTO;
	}

	@Override
	public List<HotelDTO> getHotelDTODatesCity(HotelDTO hotel) {

		List<HotelDTO> hotelsDTO = new ArrayList<HotelDTO>();

		List<Hotel> hotels = em
				.createNamedQuery(Hotel.FIND_DATES_CITY, Hotel.class)
				.setParameter("dataInizio", hotel.getDataInizio())
				.setParameter("dataFine", hotel.getDataFine())
				.setParameter("citta", hotel.getCitta()).getResultList();
		;

		for (Hotel h : hotels) {
			hotelsDTO.add(ConvertDTO.convertToHotelDTO(h));

		}

		return hotelsDTO;
	}

	@Override
	public List<HotelDTO> getHotelDTODatesCity( Date dataInizio , Date dataFine , String city) {

		List<HotelDTO> hotelsDTO = new ArrayList<HotelDTO>();

		List<Hotel> hotels = em
				.createNamedQuery(Hotel.FIND_DATES_CITY, Hotel.class)
				.setParameter("dataInizio", dataInizio)
				.setParameter("dataFine", dataFine)
				.setParameter("citta", city).getResultList();

		for (Hotel h : hotels) {
			hotelsDTO.add(ConvertDTO.convertToHotelDTO(h));
		}

		return hotelsDTO;
	}

	
	@Override
	public List<HotelDTO> getHotelDTODates(HotelDTO hotel) {

		// System.out.println("DTO passato: " + volo.toString());

		List<HotelDTO> hotelsDTO = new ArrayList<HotelDTO>();

		List<Hotel> hotels = em
				.createNamedQuery(Flight.FIND_DATES, Hotel.class)
				.setParameter("dataInizio", hotel.getDataInizio())
				.getResultList();
		;

		for (Hotel h : hotels) {
			hotelsDTO.add(ConvertDTO.convertToHotelDTO(h));

		}

		return hotelsDTO;
	}

	@Override
	public HotelDTO getHotelDTOById(int idHotel) {
		return ConvertDTO.convertToHotelDTO(em.find(Hotel.class, idHotel));
	}

}
